//
//  SDActionTitleButton.m
//  SDAlertView
//
//  Created by sander on 2017/10/24.
//

#import "SDActionTitleButton.h"

#import "SDAlertActionModel.h"


@interface SDActionTitleButton()

@property (nonatomic, strong) SDAlertActionModel * alert_model;


@property (nonatomic, weak) UILabel * theButtonTitleLabel;
@end

@implementation SDActionTitleButton

- (instancetype)initAlertModel:(SDAlertActionModel * )model
{
    self = [super init];
    if (self) {
        _alert_model = model;
        
        [self sd_configView];
    }
    return self;
}

- (void)sd_configView
{
    NSAttributedString * string = self.alert_model.alert_string;
    
    CGSize size = [string size];
    
    self.frame = CGRectMake(0, 0, size.width + sizeFromPx(60), sizeFromPx(82));

    self.theButtonTitleLabel.attributedText = string;
    
    if (self.alert_model.isSelected) {
        
        self.backgroundColor = [UIColor colorWithRed:187.0/255.0f green:23 / 255.0f blue:33.0f / 255.0f alpha:1.0f];
        
        self.layer.masksToBounds = YES;
        
        self.layer.cornerRadius = 5;
    }
}


- (UILabel *)theButtonTitleLabel
{
    if (!_theButtonTitleLabel) {
        UILabel * theView = [[UILabel alloc] init];
        [self addSubview:theView];
        theView.textAlignment = NSTextAlignmentCenter;
        theView.frame = self.bounds;
        _theButtonTitleLabel = theView;
    }
    return _theButtonTitleLabel;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.alert_model dismissViewController];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
